<template>
  <div class="center">
    <div class="flex-center">
      <div class="flex-column">
        <draggable
            class="flex-center-L"
            v-model="echartsList"
            @end="ends"
            :options="{ group: { name: 'shared', pull: 'clone', put: false }, sort: true }"
            animation="300"
            item-key="echartsType"
        >
          <template #item="{ element }">
            <div class="echarts-center" :key="element.echartsType">
              <div class="echarts-demo"></div>
              <span>{{ element.echartsType }}</span>
            </div>
          </template>
        </draggable>
      </div>
      <draggable
          class="flex-center-R"
          v-model="echartsDraggable"
          :options="{ group: { name: 'shared', pull: true, put: true }, sort: true }"
          animation="300"
          item-key="echartsType"
      >
        <template #item="{ element, index }">
          <div
              class="echarts-center"
              :class="{ 'echarts-center-color': indexEcharts === index }"
              @click="indexEcharts = index"
              :key="element.echartsType"
          >
            <div class="echarts-demo-clone"></div>
            <div class="echarts-button" v-if="indexEcharts === index">
              <el-button type="primary" icon="el-icon-edit" circle @click="edit(index)"></el-button>
              <el-button type="danger" icon="el-icon-delete" circle @click="deleteData(index)"></el-button>
            </div>
          </div>
        </template>
      </draggable>
    </div>
    <el-dialog title="配置修改" :visible.sync="dialogVisible" width="50%" :close-on-click-modal="false">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <div class="form-item-center" v-for="(domain, index) in ruleForm.echartsList" :key="index">
          <el-form-item
              label="名字"
              :prop="'echartsList.' + index + '.name'"
              :rules="{ required: true, message: '名字不能为空', trigger: 'blur' }"
          >
            <el-input v-model="domain.name"></el-input>
          </el-form-item>
          <el-form-item
              label="参数"
              :prop="'echartsList.' + index + '.value'"
              :rules="{ required: true, message: '参数不能为空', trigger: 'blur' }"
          >
            <el-input v-model="domain.value"></el-input>
          </el-form-item>
        </div>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addData">添加数据</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted, nextTick } from 'vue';
import draggable from 'vuedraggable';
import * as echarts from 'echarts';

const dialogVisible = ref(false);
const indexEcharts = ref(null);
const echartsDraggable = ref([]);
const echartsList = ref([
  { echartsType: 'line' },
  { echartsType: 'bar' },
  { echartsType: 'pie' }
]);
const ruleForm = ref({
  echartsList: [
    { name: '', value: '' },
    { name: '', value: '' }
  ]
});
const rules = {};

const myChart = ref(null);
const myChartClone = ref(null);

const init = () => {
  const doms = document.querySelectorAll('.echarts-demo');
  Array.from(doms).forEach((x, index) => {
    const echartsType = echartsList.value[index].echartsType;

    let xAxisConfig = echartsType !== 'pie' ? { type: 'category' } : { show: false, type: 'category' };
    let yAxisConfig = echartsType !== 'pie' ? { type: 'value' } : { show: false, type: 'category' };

    let seriesConfig = [{
      name: 'index',
      type: echartsType,
      data: [
        { value: 1048, name: 'Search Engine' },
        { value: 735, name: 'Direct' },
        { value: 580, name: 'Email' },
        { value: 484, name: 'Union Ads' },
        { value: 300, name: 'Video Ads' },
      ],
    }];

    myChart.value = echarts.init(x);
    myChart.value.setOption({
      tooltip: { trigger: 'item' },
      xAxis: xAxisConfig,
      yAxis: yAxisConfig,
      series: seriesConfig,
    });
  });
};

const ends = (e) => {
  console.log("111111111111");
  echartsDraggable.value = echartsDraggable.value.map(x => JSON.parse(JSON.stringify(x)));
  nextTick(() => {
    draggableEcharts(e.oldIndex, e.newIndex);
  });
};

const draggableEcharts = (oldIndex, newIndex) => {
  const doms = document.querySelectorAll('.echarts-demo-clone');
  Array.from(doms).forEach((x, index) => {
    const echartsType = echartsDraggable.value[index].echartsType;

    let xAxisConfig = echartsType !== 'pie' ? { type: 'category' } : { show: false, type: 'category' };
    let yAxisConfig = echartsType !== 'pie' ? { type: 'value' } : { show: false, type: 'category' };

    let seriesConfig = [{
      name: 'index',
      type: echartsType,
      data: echartsDraggable.value[index].echartsData || [
        { value: 1048, name: 'Search Engine' },
        { value: 735, name: 'Direct' },
        { value: 580, name: 'Email' },
        { value: 484, name: 'Union Ads' },
        { value: 300, name: 'Video Ads' },
      ],
    }];

    myChartClone.value = echarts.init(x);
    myChartClone.value.setOption({
      tooltip: { trigger: 'item' },
      xAxis: xAxisConfig,
      yAxis: yAxisConfig,
      series: seriesConfig,
    });
  });
};

const edit = (index) => {
  dialogVisible.value = true;
  nextTick(() => {
    refs.ruleForm.resetFields();
  });
};

const addData = () => {
  ruleForm.value.echartsList.push({ name: '', value: '' });
};

const submitForm = () => {
  refs.ruleForm.validate((valid) => {
    if (valid) {
      dialogVisible.value = false;
      echartsDraggable.value[indexEcharts.value].echartsData = JSON.parse(JSON.stringify(ruleForm.value.echartsList));
      draggableEcharts();
    }
  });
};

const deleteData = (index) => {
  echartsDraggable.value.splice(index, 1);
};

onMounted(() => {
  init();
  window.onresize = () => {
    if (myChart.value) myChart.value.resize();
    if (myChartClone.value) myChartClone.value.resize();
  };
});
</script>

<style scoped lang="less">
.center {
  width: 100%;
  height: 900px; /* Set the height to 900px */
  .flex-center {
    display: flex;
    width: 100%;
    height: 100%;
    .flex-column {
      width: 20%;
      height: 100%;
      display: flex;
      flex-direction: column;
      .flex-center-L {
        width: 100%;
        height: 100%;
      }
      .echarts-center {
        width: 100%;
        height: 30%;
        margin-top: 10px;
        margin-bottom: 10px;
        border: 1px solid #126fcc;
        .echarts-demo {
          width: 100%;
          height: 100%;
        }
      }
    }
    .flex-center-R {
      width: 100%;
      height: 100%;
      border: 1px solid pink;
      display: flex;
      flex-wrap: wrap;
      padding: 12px;
      margin-left: 12px;
      .echarts-center {
        width: 30%;
        height: 30%;
        border: 1px solid #126fcc;
        position: relative;
        margin-right: 12px;
        .echarts-demo-clone {
          width: 100%;
          height: 100%;
        }
        .echarts-button {
          position: absolute;
          left: 0;
          top: 0;
        }
      }
      .echarts-center-color {
        border: 1px solid red;
      }
    }
  }
  .form-item-center {
    display: flex;
    ::v-deep .el-form-item {
      width: 50%;
    }
  }
  ::v-deep .el-dialog__footer {
    text-align: center;
  }
}
</style>